﻿using Forging2.Common.Heating;
using Forging2.Services.OPCService;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Forging2.Common.Forging
{
    public class ForgingDevice : Equipment, IForging
    {
        private ForgingData ForgingData = new ForgingData();

        string[] furnaces;
        string[] forgings;
        //public List<DataBase.ZH_IMES.V_ForgingOperateData> v_ForgingOperateDatas;

        public ForgingDevice()
        {
            if (Const.Config is null)
            {
                var locAppInit = new AppInitialization();
                locAppInit.LoadConfig();
            }
            furnaces = Const.Config.FurnaceWorkCells.ToArray();
            forgings = Const.Config.ForgingWorkCells.ToArray();
        }

        public Task<dynamic> ForgingBeging(WorkingPiece workpiece)
        {
            if (Const.WorkClamping is null)
            {
                Const.WorkClamping = new Clamping.Clamping();
            }
            string msg = "", gid = "";
            workpiece = Const.WorkPieceDBToWorkpice(Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO).FirstOrDefault(), workpiece);
            if (workpiece is null)
            {
                msg = string.Format("锻件 {0} 无相关数据不能操作！", workpiece.ForgingNO);
                return Task.FromResult<dynamic>(msg);
            }
            var t = StmartWorkState(workpiece, out msg);
            t.Wait();
            gid = t.Result;
            if (gid.Length < 1)
            {
                //msg = string.Format("仅处理锻件 {0} 加热工序状态，锻压工序未开工！", workpiece.ForgingNO);
                return Task.FromResult<dynamic>(msg);
            }
            if (gid.Contains("清理"))
            {
                return Task.FromResult<dynamic>(gid);
            }

            if (workpiece is null)
            {
                //msg = string.Format("锻件：{0} 不能开工，请联系管理员！", workpiece.ForgingNO);
                //msg = gid;
                return Task.FromResult<dynamic>(msg);
            }
            else
            {
                ForgingData.WorkForging(workpiece);
                msg = string.Format("锻件：{0} 已完成开工！", workpiece.ForgingNO);
            }
            Console.WriteLine(string.Format("正在锻压的锻件ID {0} 锻件编号 {1} ", gid, workpiece.ForgingNO));
            Const.Front.Notification = msg;
            return Task.FromResult<dynamic>(msg);
        }

        public async Task<dynamic> ForgingFinshed(WorkingPiece workpiece)
        {            
            string msg = "";
            using (var tran = Const.ZH_IMESDbContext.Database.BeginTransaction())
            {
                //锻压工位已开工数据，在取料机中开工          
                var vwp = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                //开工标志不做强制要求；
                //var vwp = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                if (vwp is null)
                {
                    msg = string.Format("锻件 {0} 锻压未开工或无相关数据，不能完工！", workpiece.ForgingNO);
                    tran.Rollback();
                    return msg;
                }
                else
                {
                    var ur = Const.ZH_IMESDbContext.uex_vtrack_record.Find(vwp.GID);
                    if (ur.DIS_CODE_STATE==0)
                    {
                        msg = string.Format("锻件 {0} 锻压未开工或无相关数据，不能完工！", workpiece.ForgingNO);
                        tran.Rollback();
                        return msg;
                    }
                }

                //workpiece = Const.WorkPieceDBToWorkpice(Const.v_ForgingOperateDatas.Find(v => v.GID == vwp.GID), workpiece);
                //if (AnalysisWorkpieceState(workpiece.ForgingNO, out msg) != Const.WorkPieceState.S101)
                //{
                //    msg = string.Format("锻件编号 {0} 生产状态不正确，不能进行完工操作！", workpiece.ForgingNO);
                //    return msg;
                //}
                workpiece = Const.WorkPieceDBToWorkpice(vwp, workpiece);
                var uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(workpiece.GID);
                uvr.DIS_CODE_STATE = 1;  //开工标志
                if (uvr is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时无相关数据！", workpiece.ForgingNO);
                    tran.Rollback();
                    return msg;
                }
                var umo = Const.ZH_IMESDbContext.uex_making_order.Where(u => u.CODE == uvr.MAKING_ORDER_CODE).FirstOrDefault();
                if (umo is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时无相关数据！", workpiece.ForgingNO);
                    tran.Rollback();
                    return msg;
                }
                var upo = Const.ZH_IMESDbContext.umpp_plan_order.Find(umo.ORDER_GID);
                if (upo is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时无相关数据！", workpiece.ForgingNO);
                    tran.Rollback();
                    return msg;
                }
                DataBase.ZH_IMES.uex_daq_info udi = new DataBase.ZH_IMES.uex_daq_info()
                {
                    GID = Const.CreateGID(),
                    CODE = uvr.MAKING_ORDER_CODE,
                    ORDER_CODE = upo.CODE,
                    PRODU_CODE = uvr.PRODU_CODE,
                    PRODU_NAME = uvr.PRODU_NAME,
                    WORK_ORDER_CODE = uvr.WORK_ORDER_CODE
                    ,
                    LOT_CODE = uvr.LOT_CODE,
                    MRL_CODE = uvr.MRL_CODE,
                    SEQUENCE_NUM = 0,
                    /*DAQ_GID=null,DAQ_NAME=null,*/
                    DAQ_DATE = DateTime.Now,
                    DAQ_ID = workpiece.Operater,
                    DAQ_SRC_TYPE = 0,
                    STATION = uvr.WORK_CELL_GID,
                    WORK_CENTER_GID = uvr.WORK_CENTER_GID,
                    REPAIR_DAQ_DATE = DateTime.Now,
                    VTRACK_GID = uvr.GID,
                    OP_GID = uvr.OP_GID,
                    OP_SEQ = uvr.SEQ,
                    /*OP_STEP_GID=null,*/
                    EFFECTIVE_QTY = uvr.EFFECTIVE_QTY,
                    UNEFFECTIVE_QTY = uvr.UNEFFECTIVE_QTY,
                    DISCARD_QTY = uvr.DISCARD_QTY,
                    OPERATE_SOURCE = 0,    //操作来源(0.执行数据 1.在线返修数据 2.质检数据)
                    OPERATE_TYPE = 4,    //操作类型(0.开工 1.暂停 2.复工 3.报工 4.完工)
                    WORK_DATE = 0,
                    STOP_REASON_CODE = null,
                    LABOUR_GROUP_GID = null,
                    CONTROL_FLAG = 0,
                    REMARK = uvr.REMARK,
                    IS_FREEZE = uvr.IS_FREEZE,
                    PARENT_GID = uvr.PARENT_GID,
                    UQCM_DAQ_INFO_GID = null,
                    CREATE_DATE = DateTime.Now,
                    CREATE_ID = workpiece.Operater,
                    MODIFY_DATE = DateTime.Now,
                    MODIFY_ID = workpiece.Operater,
                    IS_ACTIVE = 0,
                    IS_DELETE = 0,
                    SITE = "*",
                };
                Const.ZH_IMESDbContext.uex_daq_info.Add(udi);

                var uto = Const.ZH_IMESDbContext.umpp_task_order.Where(u => u.CODE == uvr.DISPATCH_CODE).FirstOrDefault();
                if (uto is null)
                {
                    msg = string.Format("锻件编号 {0} {1} 工序，完工时无相关数据！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                    tran.Rollback();
                    return msg;
                }
                uto.WORK_CELL_GID = uvr.WORK_CELL_GID;
                uto.PRO_STATE = 1;
                //通过子派工单找到父派工单
                var uto2 = Const.ZH_IMESDbContext.umpp_task_order.Find(uto.UDA1);
                if (uto2 is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时无相关数据！", workpiece.ForgingNO);
                    return msg;
                }
                uto2.WORK_CELL_GID = uvr.WORK_CELL_GID;
                uto2.PRO_STATE = uto.PRO_STATE;
                //锻压工位数据
                uvr.DIS_CODE_STATE = 3;
                uvr.MODIFY_DATE = DateTime.Now;
                uvr.MODIFY_ID = workpiece.MODIFY_ID;
                uvr.UDA1 = "0"; //workpiece.InFurnace;
                uvr.ACTUAL_END_DATE = DateTime.Now;
                //加热工位数据
                var wp2 = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && furnaces.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                if (wp2 is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时无加热工序相关数据！", workpiece.ForgingNO);
                }
                else
                {
                    //加热工序完工
                    wp2.DIS_CODE_STATE = 3;
                    wp2.MODIFY_DATE = DateTime.Now;
                    wp2.MODIFY_ID = workpiece.MODIFY_ID;
                    wp2.InFurnace = "0";
                    wp2.ACTUAL_END_DATE = DateTime.Now;
                    uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(wp2.GID);
                    udi = new DataBase.ZH_IMES.uex_daq_info()
                    {
                        GID = Const.CreateGID(),
                        CODE = uvr.MAKING_ORDER_CODE,
                        ORDER_CODE = upo.CODE,
                        PRODU_CODE = uvr.PRODU_CODE,
                        PRODU_NAME = uvr.PRODU_NAME,
                        WORK_ORDER_CODE = uvr.WORK_ORDER_CODE
                   ,
                        LOT_CODE = uvr.LOT_CODE,
                        MRL_CODE = uvr.MRL_CODE,
                        SEQUENCE_NUM = 0,
                        /*DAQ_GID=null,DAQ_NAME=null,*/
                        DAQ_DATE = DateTime.Now,
                        DAQ_ID = workpiece.Operater,
                        DAQ_SRC_TYPE = 0,
                        STATION = uvr.WORK_CELL_GID,
                        WORK_CENTER_GID = uvr.WORK_CENTER_GID,
                        REPAIR_DAQ_DATE = DateTime.Now,
                        VTRACK_GID = uvr.GID,
                        OP_GID = uvr.OP_GID,
                        OP_SEQ = uvr.SEQ,
                        /*OP_STEP_GID=null,*/
                        EFFECTIVE_QTY = uvr.EFFECTIVE_QTY,
                        UNEFFECTIVE_QTY = uvr.UNEFFECTIVE_QTY,
                        DISCARD_QTY = uvr.DISCARD_QTY,
                        OPERATE_SOURCE = 0,    //操作来源(0.执行数据 1.在线返修数据 2.质检数据)
                        OPERATE_TYPE = 4,    //操作类型(0.开工 1.暂停 2.复工 3.报工 4.完工)
                        WORK_DATE = 0,
                        STOP_REASON_CODE = null,
                        LABOUR_GROUP_GID = null,
                        CONTROL_FLAG = 0,
                        REMARK = uvr.REMARK,
                        IS_FREEZE = uvr.IS_FREEZE,
                        PARENT_GID = uvr.PARENT_GID,
                        UQCM_DAQ_INFO_GID = null,
                        CREATE_DATE = DateTime.Now,
                        CREATE_ID = workpiece.Operater,
                        MODIFY_DATE = DateTime.Now,
                        MODIFY_ID = workpiece.Operater,
                        IS_ACTIVE = 0,
                        IS_DELETE = 0,
                        SITE = "*",
                    };

                    uto = Const.ZH_IMESDbContext.umpp_task_order.Where(u => u.CODE == uvr.DISPATCH_CODE).FirstOrDefault();
                    if (uto is null)
                    {
                        msg = string.Format("锻件编号 {0} {1} 工序，完工时无相关数据！", wp2.ForgingNO, wp2.WORKCELL_NAME);
                        tran.Rollback();
                        return msg;
                    }
                    uto.WORK_CELL_GID = uvr.WORK_CELL_GID;
                    uto.PRO_STATE = 1;
                    //通过子派工单找到父派工单
                    uto2 = Const.ZH_IMESDbContext.umpp_task_order.Find(uto.UDA1);
                    if (uto2 is null)
                    {
                        msg = string.Format("锻件编号 {0} 完工时无相关数据！", workpiece.ForgingNO);
                        return msg;
                    }
                    uto2.WORK_CELL_GID = uvr.WORK_CELL_GID;
                    uto2.PRO_STATE = uto.PRO_STATE;
                }
                //step 6
                umo.LAST_WORK_CELL_GID = uvr.WORK_CELL_GID;
                umo.LAST_WORK_CENTER_GID = uvr.WORK_CENTER_GID;
                umo.LAST_ACQ_EMPLOYEE_GID = Const.ZH_IMESDbContext.pmbb_employee.Where(e => e.CODE == workpiece.Operater).FirstOrDefault().GID;
                umo.LAST_OP_GID = uvr.OP_GID;
                umo.LAST_ACQ_DATE = DateTime.Now;
                umo.FINISH_DATE = DateTime.Now;
                //step 7 下道工序开工
                DataBase.ZH_IMES.pmbf_work_cell wc = Const.ZH_IMESDbContext.pmbf_work_cell.Where(c => c.CODE == Const.Config.RollingWorkCell).FirstOrDefault();
                if (wc is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时,无辗环工序相关数据！", workpiece.ForgingNO);                    
                    return msg;
                }
                DataBase.ZH_IMES.uex_vtrack_record uvrn = Const.ZH_IMESDbContext.uex_vtrack_record.Where(v => v.UDA3 == workpiece.ForgingNO && v.WORK_CELL_GID == wc.GID).FirstOrDefault();
                if (uvrn is null)
                {
                    msg = string.Format("锻件编号 {0} 完工时,无辗环工序相关数据！", workpiece.ForgingNO);                   
                    return msg;
                }
                uvrn.DIS_CODE_STATE = 1;
                int iret = await Const.ZH_IMESDbContext.SaveChangesAsync();
                tran.Commit();
                msg = string.Format("锻件编号 {0} 完工，更改 {1} 条相关数据！", workpiece.ForgingNO, iret);
                Console.WriteLine(msg);
                //不记录锻压数据
                //ForgingData.WorkForging(workpiece);
            }
            return msg;


        }

        public Task<dynamic> ForgingForging(WorkingPiece workpiece)
        {
            return Task.FromResult<dynamic>(null);
            /*
            var wr = Const.IGDataDbContext.dy_workrecord.Find(workpiece.GID);
            if (wr == null)
            {
                wr = new DataBase.QID_IGDATA_ZY.dy_workrecord();
                wr.WorkRecord_id = workpiece.GID;
                wr.ForgeNo = workpiece.ForgingNO;
                wr.ForgeName = workpiece.PRODU_NAME;
                wr.JobNo = workpiece.DIS_CODE_STATE.ToString();
                wr.Icmo_id = workpiece.ORDER_GID;
                wr.Route_id = workpiece.ROUTE_GID;
                wr.WorkCellNo = workpiece.WORKCELL_CODE;
                wr.CombineMethod =Convert.ToByte( workpiece.PORCESS_COMBINE);
                wr.CreateDate = DateTime.Now;
                wr.RecordType = 0;
                wr.State = 0;
                wr.IsDelete = 0;
                Const.IGDataDbContext.dy_workrecord.Add(wr);

                var rdd = new DataBase.QID_IGDATA_ZY.dy_workrecord_detail();
                rdd.WorkRecord_id = wr.WorkRecord_id;
                rdd.WorkRecordDetail_id =Const.CreateGID();
                rdd.SequenceNo = 1;
                if (CurrentParameter == null)
                {
                    rdd.Parameter_id = Const.IGDataDbContext.dy_plc_parameter.Where(p => p.CreateDate == Const.IGDataDbContext.dy_plc_parameter.Max(p2 => p2.CreateDate)).First().Parameter_id;
                }
                else { rdd.Parameter_id = CurrentParameter.Parameter_id; }

                rdd.HeatCount = Convert.ToByte(CurrentTask_View.HEAT_COUNT);
                rdd.WorkpieceHeight = WorkpieceHeight;
                rdd.TemperatureValue = TemperatureValue;
                rdd.WorkpieceTime = DateTime.Now;
                rdd.State = 0;
                rdd.IsDelete = 0;
                Const.IGDataDbContext.dy_workrecord_detail.Add(rdd);

                CurrentWorkRecord_id = wr.WorkRecord_id;
                CurrentWorkRecordDetail_id = rdd.WorkRecordDetail_id;
                CurrrentWorkrecordDetail = rdd;
            }
            else
            {
                int seq = Const.IGDataDbContext.dy_workrecord_detail.Where(r => r.WorkRecord_id == wr.WorkRecord_id).Count();
                var rdd = new DataBase.QID_IGDATA_ZY.dy_workrecord_detail();
                rdd.WorkRecord_id = CurrentJobNo.GID;
                rdd.WorkRecordDetail_id = CreateGID();
                CurrentSeq = Const.IGDataDbContext.dy_workrecord_detail.Where(r => r.WorkRecord_id == rdd.WorkRecord_id).Count();
                rdd.SequenceNo = ++seq;
                if (CurrentParameter == null)
                {
                    rdd.Parameter_id = Const.IGDataDbContext.dy_plc_parameter.Where(p => p.CreateDate == Const.IGDataDbContext.dy_plc_parameter.Max(i2 => i2.CreateDate)).First().Parameter_id;
                }
                else { rdd.Parameter_id = CurrentParameter.Parameter_id; }
                rdd.HeatCount = Convert.ToByte(CurrentTask_View.HEAT_COUNT);
                rdd.WorkpieceHeight = WorkpieceHeight;
                rdd.TemperatureValue = TemperatureValue;
                rdd.WorkpieceTime = DateTime.Now;
                rdd.State = 0;
                rdd.IsDelete = 0;
                Const.IGDataDbContext.dy_workrecord_detail.Add(rdd);

                CurrentWorkRecordDetail_id = rdd.WorkRecordDetail_id;
                CurrrentWorkrecordDetail = rdd;
            }

            DataSave(SaveDataType.dy);
            //更新锻压接口表
            // UpdateQIDRecord(CurrentJobNo.DISPATCH_CODE);
            var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            //CurrentWorkRecord = DataBase.V_WorkRecord.Where(p => p.CreateDate >= dt).OrderBy(p => p.BeginTime).ToList();
            //CurrentWorkRecord = DataBase.Database.SqlQuery<V_WorkRecord>(string.Format("SELECT * FROM V_WorkRecord WHERE EndTime>='{0}' ORDER BY BeginTime", dt)).ToList();
            */
        }

        public Task<string> ForgingSetZero(OpcUaClient opcuaclient, OpcNodeAttribute baseNode, OpcNodeAttribute fixNode, OpcNodeAttribute limitNode)
        {
            if (opcuaclient.Connected)
            {
                opcuaclient.WriteNodeAsync<Int16>(baseNode.Name, Int16.Parse(baseNode.Value.ToString()));
                opcuaclient.WriteNodeAsync<Int32>(fixNode.Name, int.Parse(fixNode.Value.ToString()));
                opcuaclient.WriteNodeAsync<Int32>(limitNode.Name, int.Parse(limitNode.Value.ToString()));
                ForgingData.SaveParameter(int.Parse(baseNode.Value.ToString()), int.Parse(fixNode.Value.ToString()), int.Parse(limitNode.Value.ToString()));
                return Task.FromResult("锻压机归零设置完成！");
            }
            else
            {
                return Task.FromResult("服务未连接！");
            }
        }

        public Task<int> LoadForgingOperateData()
        {
            string[] wc = Const.Config.WorkCells.ToArray<string>();
            Const.v_ForgingOperateDatas = Const.ZH_IMESDbContext.V_ForgingOperateData.Where(v => v.WORK_CENTER_GID == Const.Config.WorkCenter && v.DIS_CODE_STATE != 3 && wc.Contains(v.WORKCELL_CODE)).ToList<DataBase.ZH_IMES.V_ForgingOperateData>();

            return Task.FromResult(Const.v_ForgingOperateDatas.Count);
        }

        public Task<int> LoadTadayForgingData()
        {
            var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            //Const.v_WorkRecords=Const.IGDataDbContext.V_WorkRecord.Where(p => p.CreateDate >= dt).OrderBy(p => p.CreateDate).ToList();
            Const.v_WorkRecords = Const.IGDataDbContext.Database.SqlQuery<DataBase.QID_IGDATA_ZY.V_WorkRecord>(string.Format("SELECT * FROM V_WorkRecord where EndTime>='{0}' order by EndTime desc", dt)).ToList();
            return Task.FromResult(Const.v_WorkRecords.Count);
        }

        //public async Task<List<DataBase.ZH_IMES.v_JRL_DY1>> GetV_JRL_DY1()
        //{
        //    return await Task.FromResult(Const.ZH_IMESDbContext.v_JRL_DY1.OrderBy(v => v.FORGING_NO).ToList());
        //}

        /// <summary>
        /// 处理工件不同状态
        /// </summary>
        /// <param name="workpiece"></param>
        /// <returns></returns>
        private Task<string> StmartWorkState(WorkingPiece workpiece)
        {
            string gid = "";
            string msg = "";
            string ret = workpiece.GID;
            DataBase.ZH_IMES.V_ForgingOperateData vf;
            Task<dynamic> t;
            switch (AnalysisWorkpieceState(workpiece.ForgingNO, out gid))
            {
                case Const.WorkPieceState.S000:
                    msg = string.Format("锻件 {0} 加热初始状态S000，请先点火开工 ！", workpiece.ForgingNO);
                    ret = "";
                    break;

                case Const.WorkPieceState.S001:
                    //按正常取料流程操作
                    var icount = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO).Count();
                    //不包含2个工序的数据
                    if (icount < 2)
                    {
                        vf = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && furnaces.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                        if (vf != null)
                        {
                            gid = vf.GID;
                            vf.DIS_CODE_STATE = 3;
                            msg = string.Format("已清理 {1} 锻件编号 {0} 异常无前后工序关联数据 ！", vf.ForgingNO, vf.WORKCELL_NAME);
                        }
                        break;
                    }
                    t = Const.WorkClamping.FurnaceClamping(new HeatingFurnace(workpiece.WORK_CELL_GID), workpiece);
                    t.Wait();
                    ret = t.Result;
                    if (ret.Length == 0)
                    {
                        msg = string.Format("锻件 {0} 出炉操作遇到问题！", workpiece.ForgingNO);
                        break;
                    }
                    Console.WriteLine(string.Format("锻件 {0} 装炉取料完成！", ret));
                    workpiece = Const.WorkPieceDBToWorkpice(Const.v_ForgingOperateDatas.FirstOrDefault(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)), workpiece);
                    t = Const.WorkClamping.ForgingLoosen(new ForgingDevice(), workpiece);
                    t.Wait();
                    ret = t.Result;
                    if (ret.Length == 0)
                    {
                        msg = string.Format("锻件 {0} 出炉操作遇到问题！锻压工序未找到相应派工单", workpiece.ForgingNO);
                        break;
                    }
                    Console.WriteLine(string.Format("锻件 {0} 锻压机放料完成！", ret));
                    msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                    break;

                case Const.WorkPieceState.S002:
                    //此数据一般为垃圾数据，此类型数据直接修改完成状态，不按标准的完工流程操作。
                    if (workpiece.DIS_CODE_STATE == 2)
                    {
                        var r = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                        if (r == null)
                        {
                            workpiece.DIS_CODE_STATE = 3;
                            vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                            vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                            msg = string.Format("已清理 {1} 锻件编号 {0} 异常加热状态（S002）数据 ！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                        }
                        else //正常暂停数据
                        {
                            //锻压工序开工
                            r.DIS_CODE_STATE = 1;
                            r.ACTUAL_BEGIN_DATE = DateTime.Now;
                            r = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                            workpiece.InFurnace = 1.ToString();
                            r.InFurnace = workpiece.InFurnace;
                            
                            msg = string.Format("锻件编号 {0} 在 {1} 恢复正常加热生产状态！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                        }

                    }
                    break;

                case Const.WorkPieceState.S003:
                    msg = string.Format("锻件 {0} 加热工序已经完工，无需再次操作！", workpiece.ForgingNO);
                    ret = "";
                    break;

                case Const.WorkPieceState.S010:
                    workpiece.DIS_CODE_STATE = 1;
                    vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                    vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                    msg = string.Format("{1} 锻件编号 {0} 加热 开工完成！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                    msg = string.Format("锻件 {0} 加热异常状态 S010 ！", workpiece.ForgingNO);
                    ret = "";
                    break;

                case Const.WorkPieceState.S011: //正常开始状态加热炉取料
                    //---->MES 出炉操作，后台数据变更
                    gid = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && v.FurnaceOperateType != null).FirstOrDefault().GID;
                    if (gid != null)
                    {
                        //加热炉工位出炉变更
                        vf = Const.v_ForgingOperateDatas.Find(v => v.GID == gid);
                        workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                        vf.InFurnace = "0";
                        vf.MODIFY_DATE = DateTime.Now;
                        vf.MODIFY_ID = workpiece.Operater;

                    }
                    var ufhr = Const.ZH_IMESDbContext.uex_furnace_handle_record.Where(u => u.VTRACK_RECORD_GID == gid).FirstOrDefault();
                    if (ufhr != null)
                    {
                        ufhr.TYPE = 2;
                        ufhr.MODIFY_DATE = DateTime.Now;
                        workpiece.HEAT_COUNT = ++ufhr.HEAT_COUNT;
                    }

                    //<-----
                    t = Const.WorkClamping.FurnaceClamping(new HeatingFurnace(workpiece.WORK_CELL_GID), workpiece);
                    t.Wait();
                    ret = Convert.ToString(t.Result);
                    Console.WriteLine(string.Format("锻件 {0} 装炉取料完成！", ret));
                    if (ret.Length == 0)
                    {
                        msg = string.Format("锻件 {0} 出炉操作遇到问题！", workpiece.ForgingNO);
                        break;
                    }
                    //锻压机工位
                    vf = Const.v_ForgingOperateDatas.FirstOrDefault(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE));
                    workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                    t = Const.WorkClamping.ForgingLoosen(new ForgingDevice(), workpiece);
                    t.Wait();
                    ret = t.Result;
                    Console.WriteLine(string.Format("锻件 {0} 锻压机放料完成！", ret));
                    if (ret.Length == 0)
                    {
                        msg = string.Format("锻件 {0} 出炉操作遇到问题！锻压工序未找到相应派工单", workpiece.ForgingNO);
                        break;
                    }
                    msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                    workpiece.InFurnace = "0";
                    workpiece.MODIFY_DATE = DateTime.Now;
                    workpiece.MODIFY_ID = Const.Config.OperateCode;
                    vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                    vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                    vf.InFurnace = workpiece.InFurnace;
                    vf.MODIFY_DATE = workpiece.MODIFY_DATE;
                    vf.MODIFY_ID = workpiece.MODIFY_ID;
                    vf.HEAT_COUNT = workpiece.HEAT_COUNT;
                    Console.WriteLine(string.Format("出炉操作修改 {0} 条数据！", Const.ZH_IMESDbContext.SaveChanges()));

                    break;
                case Const.WorkPieceState.S012: //作为正常开工取料操作
                    workpiece.DIS_CODE_STATE = 1;
                    vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                    vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                    msg = string.Format("锻件 {0} 加热异常状态 S012 ！", workpiece.ForgingNO);
                    //if (ret.Length == 0)
                    //{
                    //    msg = string.Format("锻件 {0} 出炉操作遇到问题！锻压工序未找到相应派工单", workpiece.ForgingNO);
                    //    break;
                    //}
                    //msg = string.Format("锻件 {0} 锻压工序开工完成！", workpiece.ForgingNO);
                    ret = "";
                    break;

                case Const.WorkPieceState.S013: //
                    msg = string.Format("锻件 {0} 加热异常状态 S013 ！", workpiece.ForgingNO);
                    ret = "";
                    break;
                case Const.WorkPieceState.S100: //锻件开工操作
                    workpiece = Const.WorkPieceDBToWorkpice(Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault(), workpiece);
                    if (workpiece is null)
                    {
                        msg = string.Format("锻件 {0} 相关数据异常！", workpiece.ForgingNO);
                        ret = "";
                        break;
                    }
                    workpiece.DIS_CODE_STATE = 1;
                    vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                    vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                    ForgingData.WorkForging(workpiece);
                    msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                    break;

                case Const.WorkPieceState.S101:
                    ForgingData.WorkForging(workpiece);
                    msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                    break;

                case Const.WorkPieceState.S102:
                    workpiece.DIS_CODE_STATE = 1;
                    vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                    vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                    ForgingData.WorkForging(workpiece);
                    msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                    break;

                case Const.WorkPieceState.S103:
                    msg = string.Format("锻件编号 {0} 锻压工序已经完工，无需再次完工！", workpiece.ForgingNO);
                    ret = "";
                    break;
                default:
                    Console.WriteLine("未检测到锻件处所生产状态！");
                    msg = "未检测到锻件处所生产状态！";
                    ret = "";
                    break;

            }
            //Const.Front.Notification = msg;
            return Task.FromResult(msg);
        }

        private Task<string> StmartWorkState(WorkingPiece workpiece, out string msg)
        {
            string retgid = workpiece.GID;
            msg = "";
            DataBase.ZH_IMES.V_ForgingOperateData vf;
            DataBase.ZH_IMES.uex_vtrack_record uvr;
            Task<dynamic> t;
            Const.WorkPieceState state = AnalysisWorkpieceState(workpiece.ForgingNO, out retgid);
            System.Data.Entity.DbContextTransaction tran = Const.ZH_IMESDbContext.Database.BeginTransaction();
            int icount = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO).Count();
            if (icount == 0)
            {
                msg = string.Format("");
                tran.Rollback();
                return Task.FromResult("");
            }
            if (state==Const.WorkPieceState.S000)
            {
                msg = string.Format("锻件 {0} 不能开工，请先在MES中进行进炉开工操作！",workpiece.ForgingNO);
                tran.Rollback();
                return Task.FromResult("");
            }
            if (state.ToString().Substring(0, 2) == "S0")
            {
                //不评估炉内数据状态如何都统一处理，不再做2次处理。
                vf = Const.v_ForgingOperateDatas.Find(v => v.GID == retgid);
                uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(retgid);
                if (icount == 1)
                {
                    //vf.DIS_CODE_STATE = 3;
                    uvr.DIS_CODE_STATE = 3;
                    msg = string.Format("已清理 {1} 锻件编号 {0} 异常加热状态 {2} 数据 ！", workpiece.ForgingNO, workpiece.WORKCELL_NAME,state);
                    return Task.FromResult("");
                }
                else
                {
                    var vfg = Const.v_ForgingOperateDatas.Find(v => v.ForgingNO == workpiece.ForgingNO && v.FurnaceOperateType != null);
                    //加热炉进出炉操作记录
                    if (vfg == null)
                    {
                        msg = string.Format("锻件 {0} 不能开工，请先在MES中进行进炉、开工操作！", workpiece.ForgingNO);
                        tran.Rollback();
                        return Task.FromResult("");
                    }
                    else 
                    { 
                        //加热炉工位出炉变更                       
                        retgid = vfg.GID;
                        var ufhr = Const.ZH_IMESDbContext.uex_furnace_handle_record.Where(u => u.VTRACK_RECORD_GID == retgid).FirstOrDefault();
                        if (ufhr != null)
                        {
                            ufhr.TYPE = 2;
                            ufhr.MODIFY_DATE = DateTime.Now;
                            //vf.HEAT_COUNT = ++ufhr.HEAT_COUNT;
                            ++ufhr.HEAT_COUNT;
                            ufhr.OUT_TIME = DateTime.Now;
                        }
                        uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(retgid);
                        uvr.UDA1 = "0";
                        uvr.MODIFY_DATE = DateTime.Now;
                        uvr.MODIFY_ID = workpiece.Operater;
                    }


                    //-----开始出炉---
                    workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                    t = Const.WorkClamping.FurnaceClamping(new HeatingFurnace(workpiece.WORK_CELL_GID), workpiece);
                    t.Wait();
                    retgid = Convert.ToString(t.Result);
                    Console.WriteLine(string.Format("锻件 {0} 装炉取料完成！", retgid));
                    if (retgid.Length == 0)
                    {
                        msg = string.Format("锻件 {0} 出炉操作遇到问题！", workpiece.ForgingNO);
                        //workpiece.DIS_CODE_STATE = 1;
                        //vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                        uvr.DIS_CODE_STATE= 1;

                    }
                    //锻压机工位
                    vf = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                    uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(vf.GID);
                    if (vf == null)
                    {
                        msg = string.Format("锻件 {0} 锻压无法开工！", workpiece.ForgingNO);
                        tran.Rollback();
                        return Task.FromResult("");
                    }
                    workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                    t = Const.WorkClamping.ForgingLoosen(new ForgingDevice(), workpiece);
                    t.Wait();
                    retgid = t.Result;
                    Console.WriteLine(string.Format("锻件 {0} 锻压机放料完成！", retgid));
                    if (retgid.Length == 0)
                    {
                        msg = string.Format("锻件 {0} 开工操作遇到问题！锻压工序未找到相应派工单！", workpiece.ForgingNO);
                        //vf.DIS_CODE_STATE = 1;
                        uvr.DIS_CODE_STATE = 1;

                    }
                    msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                    workpiece.InFurnace = "0";
                    workpiece.MODIFY_DATE = DateTime.Now;
                    workpiece.MODIFY_ID = Const.Config.OperateCode;
                    //vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                    //vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                    //vf.InFurnace = workpiece.InFurnace;
                    //vf.MODIFY_DATE = workpiece.MODIFY_DATE;
                    //vf.MODIFY_ID = workpiece.MODIFY_ID;
                    //vf.HEAT_COUNT = workpiece.HEAT_COUNT;

                }
            }
            if (state.ToString().Substring(0, 2) == "S1")
            {
                vf = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                if (vf is null)
                {
                    msg = string.Format("锻件 {0} 相关数据异常！", workpiece.ForgingNO);
                    tran.Rollback();
                    Task.FromResult("");
                }
                workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(vf.GID);
                if (uvr is null)
                {
                    msg = string.Format("锻件 {0} 相关数据异常！", workpiece.ForgingNO);
                    tran.Rollback();
                    Task.FromResult("");
                }
                workpiece.DIS_CODE_STATE = 1;
                //vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                //vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                uvr = Const.ZH_IMESDbContext.uex_vtrack_record.Find(vf.GID);
                uvr.DIS_CODE_STATE = 1;
                ForgingData.WorkForging(workpiece);
                msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);

            }
            Console.WriteLine(string.Format("出炉操作修改 {0} 条数据！", Const.ZH_IMESDbContext.SaveChanges()));
            tran.Commit();
            return Task.FromResult(retgid);

            #region 旧代码简化之后不再使用
            /*************************************************************************************************

          switch (AnalysisWorkpieceState(workpiece.ForgingNO, out retgid))
          {
              case Const.WorkPieceState.S000:
                  msg = string.Format("锻件 {0} 加热初始状态S000，请先点火开工 ！", workpiece.ForgingNO);
                  retgid = "";
                  break;

              case Const.WorkPieceState.S001:
                  //按正常取料流程操作
                   icount = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO).Count();
                  //不包含2个工序的数据
                  if (icount < 2)
                  {
                      vf = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && furnaces.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                      if (vf != null)
                      {
                          retgid = vf.GID;
                          vf.DIS_CODE_STATE = 3;
                          msg = string.Format("已清理 {1} 锻件编号 {0} 异常无前后工序关联数据 ！", vf.ForgingNO, vf.WORKCELL_NAME);
                      }
                      break;
                  }
                  t = Const.WorkClamping.FurnaceClamping(new HeatingFurnace(workpiece.WORK_CELL_GID), workpiece);
                  t.Wait();
                  retgid = t.Result;
                  if (retgid.Length == 0)
                  {
                      msg = string.Format("锻件 {0} 出炉操作遇到问题！", workpiece.ForgingNO);
                      break;
                  }
                  Console.WriteLine(string.Format("锻件 {0} 装炉取料完成！", retgid));
                  workpiece = Const.WorkPieceDBToWorkpice(Const.v_ForgingOperateDatas.FirstOrDefault(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)), workpiece);
                  t = Const.WorkClamping.ForgingLoosen(new ForgingDevice(), workpiece);
                  t.Wait();
                  retgid = t.Result;
                  if (retgid.Length == 0)
                  {
                      msg = string.Format("锻件 {0} 出炉操作遇到问题！锻压工序未找到相应派工单", workpiece.ForgingNO);
                      break;
                  }
                  Console.WriteLine(string.Format("锻件 {0} 锻压机放料完成！", retgid));
                  msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                  break;

              case Const.WorkPieceState.S002:
                  //此数据一般为垃圾数据，此类型数据直接修改完成状态，不按标准的完工流程操作。
                  //炉内数据
                  vf = Const.v_ForgingOperateDatas.Find(v => v.ForgingNO == workpiece.ForgingNO && furnaces.Contains(v.WORKCELL_CODE));
                  var r = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault();
                  if (r == null)
                  {
                      vf.DIS_CODE_STATE = 3;                        
                      msg = string.Format("已清理 {1} 锻件编号 {0} 异常加热状态（S002）数据 ！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                      retgid = "";
                  }
                  else //正常暂停数据
                  {
                      //锻压工序开工
                      r.DIS_CODE_STATE = 1;
                      r.ACTUAL_BEGIN_DATE = DateTime.Now;
                      vf.InFurnace = "1";
                      vf.DIS_CODE_STATE = 1;

                      msg = string.Format("锻件编号 {0} 在 {1} 恢复正常加热生产状态！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                      retgid = r.GID;
                  }


                  break;

              case Const.WorkPieceState.S003:
                  msg = string.Format("锻件 {0} 加热工序已经完工，无需再次操作！", workpiece.ForgingNO);
                  retgid = "";
                  break;

              case Const.WorkPieceState.S010:
                  vf = Const.v_ForgingOperateDatas.Find(v => v.ForgingNO == workpiece.ForgingNO && furnaces.Contains(v.WORKCELL_CODE));
                  vf.DIS_CODE_STATE = 1;
                  msg = string.Format("{1} 锻件编号 {0} 加热 开工完成！", workpiece.ForgingNO, workpiece.WORKCELL_NAME);
                  msg = string.Format("锻件 {0} 加热异常状态 S010 ！", workpiece.ForgingNO);
                  retgid = "";
                  break;

              case Const.WorkPieceState.S011: //正常开始状态加热炉取料
                  //---->MES 出炉操作，后台数据变更
                  retgid = Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && v.FurnaceOperateType != null).FirstOrDefault().GID;
                  if (retgid != null)
                  {
                      //加热炉工位出炉变更
                      vf = Const.v_ForgingOperateDatas.Find(v => v.GID == retgid);
                      workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                      vf.InFurnace = "0";
                      vf.MODIFY_DATE = DateTime.Now;
                      vf.MODIFY_ID = workpiece.Operater;

                  }
                  var ufhr = Const.ZH_IMESDbContext.uex_furnace_handle_record.Where(u => u.VTRACK_RECORD_GID == retgid).FirstOrDefault();
                  if (ufhr != null)
                  {
                      ufhr.TYPE = 2;
                      ufhr.MODIFY_DATE = DateTime.Now;
                      workpiece.HEAT_COUNT = ++ufhr.HEAT_COUNT;
                  }

                  //<-----
                  t = Const.WorkClamping.FurnaceClamping(new HeatingFurnace(workpiece.WORK_CELL_GID), workpiece);
                  t.Wait();
                  retgid = Convert.ToString(t.Result);
                  Console.WriteLine(string.Format("锻件 {0} 装炉取料完成！", retgid));
                  if (retgid.Length == 0)
                  {
                      msg = string.Format("锻件 {0} 出炉操作遇到问题！", workpiece.ForgingNO);
                      break;
                  }
                  //锻压机工位
                  vf = Const.v_ForgingOperateDatas.FirstOrDefault(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE));
                  workpiece = Const.WorkPieceDBToWorkpice(vf, workpiece);
                  t = Const.WorkClamping.ForgingLoosen(new ForgingDevice(), workpiece);
                  t.Wait();
                  retgid = t.Result;
                  Console.WriteLine(string.Format("锻件 {0} 锻压机放料完成！", retgid));
                  if (retgid.Length == 0)
                  {
                      msg = string.Format("锻件 {0} 出炉操作遇到问题！锻压工序未找到相应派工单", workpiece.ForgingNO);
                      break;
                  }
                  msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                  workpiece.InFurnace = "0";
                  workpiece.MODIFY_DATE = DateTime.Now;
                  workpiece.MODIFY_ID = Const.Config.OperateCode;
                  vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                  vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                  vf.InFurnace = workpiece.InFurnace;
                  vf.MODIFY_DATE = workpiece.MODIFY_DATE;
                  vf.MODIFY_ID = workpiece.MODIFY_ID;
                  vf.HEAT_COUNT = workpiece.HEAT_COUNT;
                  Console.WriteLine(string.Format("出炉操作修改 {0} 条数据！", Const.ZH_IMESDbContext.SaveChanges()));

                  break;
              case Const.WorkPieceState.S012: //作为正常开工取料操作
                  vf = Const.v_ForgingOperateDatas.Find(v => v.ForgingNO == workpiece.ForgingNO && furnaces.Contains(v.WORKCELL_CODE));
                  vf.DIS_CODE_STATE = 1;
                  msg = string.Format("锻件 {0} 加热异常状态 S012 ！", workpiece.ForgingNO);
                  //if (ret.Length == 0)
                  //{
                  //    msg = string.Format("锻件 {0} 出炉操作遇到问题！锻压工序未找到相应派工单", workpiece.ForgingNO);
                  //    break;
                  //}
                  //msg = string.Format("锻件 {0} 锻压工序开工完成！", workpiece.ForgingNO);
                  retgid = "";
                  break;

              case Const.WorkPieceState.S013: //
                  msg = string.Format("锻件 {0} 加热异常状态 S013 ！", workpiece.ForgingNO);
                  retgid = "";
                  break;
              case Const.WorkPieceState.S100: //锻件开工操作
                  workpiece = Const.WorkPieceDBToWorkpice(Const.v_ForgingOperateDatas.Where(v => v.ForgingNO == workpiece.ForgingNO && forgings.Contains(v.WORKCELL_CODE)).FirstOrDefault(), workpiece);
                  if (workpiece is null)
                  {
                      msg = string.Format("锻件 {0} 后台数据异常！", workpiece.ForgingNO);
                      retgid = "";
                      break;
                  }
                  workpiece.DIS_CODE_STATE = 1;
                  vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                  vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                  ForgingData.WorkForging(workpiece);
                  msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                  break;

              case Const.WorkPieceState.S101:
                  ForgingData.WorkForging(workpiece);
                  msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                  break;

              case Const.WorkPieceState.S102:
                  workpiece.DIS_CODE_STATE = 1;
                  vf = Const.ZH_IMESDbContext.V_ForgingOperateData.Find(workpiece.GID);
                  vf.DIS_CODE_STATE = workpiece.DIS_CODE_STATE;
                  ForgingData.WorkForging(workpiece);
                  msg = string.Format("锻件 {0} 锻压工序已开工！", workpiece.ForgingNO);
                  break;

              case Const.WorkPieceState.S103:
                  msg = string.Format("锻件编号 {0} 锻压工序已经完工，无需再次完工！", workpiece.ForgingNO);
                  retgid = "";
                  break;
              default:
                  Console.WriteLine("未检测到锻件处所生产状态！");
                  msg = "未检测到锻件处所生产状态！";
                  retgid = "";
                  break;

          }
          //Const.Front.Notification = msg;
          return Task.FromResult(retgid);
          ****/
            #endregion

        }

        /// <summary>
        /// 锻件状态
        /// </summary>
        /// <param name="gForgingNo">锻件编号</param>
        /// <returns>00—未在炉未开工，01—未在炉已开工，02—未在炉已暂停，03—未在炉已完工 10—在炉未开工，11—在炉已开工，12—在炉已暂停，13—在炉已完工
        ///          20—锻造未开工，21—锻造已开工，22—锻造已暂停，23—锻造已完工</returns>
        private Const.WorkPieceState AnalysisWorkpieceState(string gForgingNo, out string gGID)
        {
            int iret = 0;
            string[] furnaces = Const.Config.FurnaceWorkCells.ToArray();
            string[] forgings = Const.Config.ForgingWorkCells.ToArray();
            Const.WorkPieceState workPieceState = Const.WorkPieceState.SXXX;
            //OutFurnace 加热工序锻件未在炉内暂不做处理
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 0 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 0 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S000;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S000;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 1 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 1 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S001;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S001;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 2 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 2 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S002;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S002;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 3 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 3 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S003;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S003;
            }
            //InFurnace
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 0 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 0 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S010;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S010;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 1 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 1 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S011;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S011;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 2 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 2 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S012;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S012;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 3 && furnaces.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "1" && c.DIS_CODE_STATE == 3 && furnaces.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S013;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return Const.WorkPieceState.S013;
            }
            //Forging
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 0 && forgings.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 0 && forgings.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S100;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return workPieceState;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 1 && forgings.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 1 && forgings.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S101;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return workPieceState;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 2 && forgings.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 2 && forgings.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S102;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return workPieceState;
            }
            iret = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 3 && forgings.Contains(c.WORKCELL_CODE)).Count();
            if (iret > 0)
            {
                gGID = Const.v_ForgingOperateDatas.Where(c => c.ForgingNO == gForgingNo && c.InFurnace == "0" && c.DIS_CODE_STATE == 3 && forgings.Contains(c.WORKCELL_CODE)).FirstOrDefault().GID;
                workPieceState = Const.WorkPieceState.S103;
                Console.WriteLine(gForgingNo + " 状态是： " + workPieceState);
                return workPieceState;
            }

            gGID = "";
            return Const.WorkPieceState.SXXX;
        }




    }
}